<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="UTF-8" />
    <title>Modal - canDismiss</title>
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"
    />
    <link href="../../../../../css/ionic.bundle.css" rel="stylesheet" />
    <link href="../../../../../scripts/testing/styles.css" rel="stylesheet" />
    <script src="../../../../../scripts/testing/scripts.js"></script>
    <script type="module" src="../../../../../dist/ionic/ionic.esm.js"></script>

    <style>
      form {
        display: inline-block;
        margin: 0 20px 20px 0;
      }
    </style>
  </head>
  <script type="module">
    import { modalController, actionSheetController } from '../../../../dist/ionic/index.esm.js';
    window.modalController = modalController;
    window.actionSheetController = actionSheetController;
  </script>

  <body>
    <ion-app>
      <div class="root-page ion-page">
        <ion-header>
          <ion-toolbar>
            <ion-title>Modal - canDismiss</ion-title>
          </ion-toolbar>
        </ion-header>

        <ion-content class="ion-padding">
          <button
            id="sheet-can-dismiss-true"
            onclick="createModal({ canDismiss: true, breakpoints: BREAKPOINTS, initialBreakpoint: BREAKPOINTS[1]  })"
          >
            Sheet Modal / canDismiss = true
          </button>
          <button
            id="sheet-can-dismiss-false"
            onclick="createModal({ canDismiss: false, breakpoints: BREAKPOINTS, initialBreakpoint: BREAKPOINTS[1]  })"
          >
            Sheet Modal / canDismiss = false
          </button>
          <button
            id="sheet-can-dismiss-promise-true"
            onclick="createModal({ canDismiss: PROMISE_TRUE, breakpoints: BREAKPOINTS, initialBreakpoint: BREAKPOINTS[1]  })"
          >
            Sheet Modal / canDismiss = Promise(true)
          </button>
          <button
            id="sheet-can-dismiss-promise-false"
            onclick="createModal({ canDismiss: PROMISE_FALSE, breakpoints: BREAKPOINTS, initialBreakpoint: BREAKPOINTS[1]  })"
          >
            Sheet Modal / canDismiss = Promise(false)
          </button>

          <button id="card-can-dismiss-true" onclick="createModal({ canDismiss: true, presentingElement: rootPage })">
            Card Modal / canDismiss = true
          </button>
          <button id="card-can-dismiss-false" onclick="createModal({ canDismiss: false, presentingElement: rootPage })">
            Card Modal / canDismiss = false
          </button>
          <button
            id="card-can-dismiss-promise-true"
            onclick="createModal({ canDismiss: PROMISE_TRUE, presentingElement: rootPage  })"
          >
            Card Modal / canDismiss = Promise(true)
          </button>
          <button
            id="card-can-dismiss-promise-false"
            onclick="createModal({ canDismiss: PROMISE_FALSE, presentingElement: rootPage  })"
          >
            Card Modal / canDismiss = Promise(false)
          </button>
        </ion-content>
      </div>
    </ion-app>

    <script>
      const BREAKPOINTS = [0, 1];
      const PROMISE_TRUE = (data, role) => {
        return new Promise((resolve) => {
          setTimeout(() => {
            const event = new CustomEvent('ionHandlerDone', { detail: { data, role } });
            window.dispatchEvent(event);
            resolve(true);
          }, 50);
        });
      };
      const PROMISE_FALSE = (data, role) => {
        return new Promise((resolve) => {
          setTimeout(() => {
            const event = new CustomEvent('ionHandlerDone', { detail: { data, role } });
            window.dispatchEvent(event);
            resolve(false);
          }, 50);
        });
      };
      const rootPage = document.querySelector('.root-page');

      const createModal = async (opts) => {
        const el = document.createElement('div');
        el.innerHTML = `
          <ion-header id="modal-header">
            <ion-toolbar>
              <ion-title>Modal</ion-title>
            </ion-toolbar>
          </ion-header>
          <ion-content class="ion-padding">
            Modal with canDismiss
          </ion-content>
        `;

        const modal = await modalController.create({
          component: el,
          ...opts,
        });

        await modal.present();
      };
    </script>
  </body>
</html>
